<script setup>
import { ref, watch } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { userRegisterService, userLoginService } from '@/api/user'

import { useUserStore } from '@/stores'
const userStore = useUserStore()

import { useRouter } from 'vue-router'
const router = useRouter()

// 登录注册 切换
const isRegister = ref(false)
// 切换后还原表单数据
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
const ban = () => ElMessage.warning('暂时-禁止注册')

// 表单提交数据
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})

// 注册
const registerBtn = async () => {
  await form.value.validate()
  console.log('注册')
  const res = await userRegisterService(formModel.value)
  // console.log(res.data)
  if (res.data.code === 0) {
    ElMessage.success('注册成功！')
    isRegister.value = false
    return
  }
  if (res.data.code === 1) {
    ElMessage.error('用户名被占用，请更换其他用户名！')
    return
  }
  if (res.data.code === 2) {
    ElMessage.error('用户名或密码不能为空！')
    return
  }
}
// 登陆
const loginBtn = async () => {
  await form.value.validate()
  console.log('登录')
  const res = await userLoginService(formModel.value)
  // console.log(res.data)
  if (res.data.code === 0) {
    ElMessage.success('登陆成功！')
    userStore.setToken(res.data.token)
    // 进入首页
    router.push('/')
    return
  }
  if (res.data.code === 1) {
    ElMessage.error('用户名或密码错误！')
    return
  }
  if (res.data.code === 2) {
    ElMessage.error('用户名或密码不能为空！')
    return
  }
}

// 表单校验规则
const form = ref()
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是 5-10位 的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入再次密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rules, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
</script>

<template>
  <div class="login-page">
    <el-container>
      <el-header>
        <h1>喵星汇-云笔记</h1>
      </el-header>
      <el-main>
        <!-- 注册 -->
        <div v-if="isRegister">
          <el-form ref="form" :model="formModel" :rules="rules">
            <el-form-item>
              <h2>注册</h2>
            </el-form-item>
            <el-form-item prop="username">
              <el-input
                :prefix-icon="User"
                placeholder="请输入用户名"
                v-model="formModel.username"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :prefix-icon="Lock"
                placeholder="请输入密码"
                show-password
                v-model="formModel.password"
              />
            </el-form-item>
            <el-form-item prop="repassword">
              <el-input
                :prefix-icon="Lock"
                placeholder="请输入再次密码"
                show-password
                v-model="formModel.repassword"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" link @click="isRegister = false">
                ←返回登陆
              </el-button>
              <el-button type="warning" @click="registerBtn()">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 登陆 -->
        <div v-else>
          <el-form ref="form" :model="formModel" :rules="rules">
            <el-form-item>
              <h2>登陆</h2>
            </el-form-item>
            <el-form-item prop="username">
              <el-input
                :prefix-icon="User"
                placeholder="请输入用户名"
                v-model="formModel.username"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :prefix-icon="Lock"
                placeholder="请输入密码"
                show-password
                v-model="formModel.password"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="loginBtn()">登陆</el-button>
              <el-button @click="ban()">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
      <el-footer>
        <p class="copyright">© 2024 | 喵星汇</p>
        <p class="version">版本：0.0.1</p>
      </el-footer>
    </el-container>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  .el-container {
    height: 100vh;
    .el-header {
      text-align: center;
    }
    .el-main {
      background-color: #f6f6f6;
    }
    .el-footer {
      .copyright {
        text-align: center;
      }
      .version {
        text-align: right;
      }
    }
  }
}
</style>
